package com.wjx.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjx.dtos.ResponseResult;
import com.wjx.dtos.UserDTO;
import com.wjx.euums.AppHttpCodeEnum;
import com.wjx.pojo.Accounts;
import com.wjx.pojo.Customers;
import com.wjx.pojo.Deposits;
import com.wjx.service.AccountsService;
import com.wjx.service.CustomersService;
import com.wjx.service.DepositsService;
import com.wjx.mapper.DepositsMapper;
import com.wjx.utils.ThreadLocalUtil;
import com.wjx.vos.SaveVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* @author WJX
* @description 针对表【deposits】的数据库操作Service实现
* @createDate 2024-06-25 17:08:34
*/
@Service
@Slf4j
public class DepositsServiceImpl extends ServiceImpl<DepositsMapper, Deposits>
    implements DepositsService{
    @Autowired
    private AccountsService accountsService;
    @Autowired
    private DepositsMapper depositsMapper;
    @Autowired
    private CustomersService customersService; // 注入账户服务

    @Override
    public ResponseResult savemoney(Deposits deposits) {
        // 先判断账户是否存在
        UserDTO user = ThreadLocalUtil.getUser();
        QueryWrapper<Customers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Name", user.getName());
        List<Customers> list = customersService.list(queryWrapper);
        if(list.isEmpty()){
            log.info("用户不存在");
            Customers customers = new Customers();
            BeanUtils.copyProperties(user, customers);
            customersService.save(customers);
            Integer customerid = customers.getCustomerid();
            Accounts accounts = new Accounts();
            accounts.setCustomerid(customerid);
            accounts.setOpeningdate(DateUtil.date());
            accounts.setBalance(new BigDecimal("0"));
            accounts.setAccounttype("3");
            accountsService.save(accounts);
            System.out.println(DateUtil.date());
            if (accounts.getAccounttype().equals("1"))
            {
                deposits.setInterestrate(new BigDecimal("3.00"));
            }
            else if (accounts.getAccounttype().equals("2"))
            {
                deposits.setInterestrate(new BigDecimal("5.00"));
            }
            else if (accounts.getAccounttype().equals("3"))
            {
                deposits.setInterestrate(new BigDecimal("0.00"));
            }
            else {
                log.error("账户类型错误");
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
            deposits.setAccountid(accounts.getAccountid());
            deposits.setDepositdate(DateUtil.date());
            depositsMapper.insert(deposits);
            // 然后更新账户余额
            accounts.setBalance(accounts.getBalance().add(new BigDecimal(deposits.getAmount())));
            accountsService.updateById(accounts);
            SaveVO saveVO = new SaveVO();
            saveVO.setBalance(accounts.getBalance());
            return ResponseResult.okResult(saveVO);
        }
        else {
            log.info("用户存在");
            Accounts account = accountsService.getOne(new QueryWrapper<Accounts>().eq("CustomerID", list.get(0).getCustomerid()));
            BigDecimal rate = new BigDecimal("0.00");
            //存款
            if (account.getAccounttype().equals("1"))
            {
                deposits.setInterestrate(new BigDecimal("0.01"));
                rate = new BigDecimal("0.01");
            }
            else if (account.getAccounttype().equals("2"))
            {
                deposits.setInterestrate(new BigDecimal("0.02"));
                rate = new BigDecimal("0.02");
            }
            else if (account.getAccounttype().equals("3"))
            {
                deposits.setInterestrate(new BigDecimal("0.00"));
                rate = new BigDecimal("0.00");
            }
            else {
                log.error("账户类型错误");
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
            deposits.setAccountid(account.getAccountid());
            deposits.setDepositdate(DateUtil.date());
            depositsMapper.insert(deposits);
            // 然后更新账户余额
            Date openingdate = account.getOpeningdate();
            long between = DateUtil.between(openingdate, DateUtil.date(), DateUnit.DAY);
            for (long i = 0; i < between; i++) {
                account.setBalance(account.getBalance().add(account.getBalance().multiply(rate)));
            }
            account.setBalance(account.getBalance().add(new BigDecimal(deposits.getAmount())));
            accountsService.updateById(account);
            SaveVO saveVO = new SaveVO();
            saveVO.setBalance( accountsService.getOne(new QueryWrapper<Accounts>().eq("CustomerID", list.get(0).getCustomerid())).getBalance());
            return ResponseResult.okResult(saveVO);
        }

    }
}




